package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.QsUserDao;
import com.djinfo.work.entity.QsQuestionEntity;
import com.djinfo.work.entity.QsUserEntity;
import com.djinfo.work.service.QsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service("qsUserService")
public class QsUserServiceImpl implements QsUserService {
	@Autowired
	private QsUserDao qsUserDao;



	@Override
	public void saveOrUpdate(QsUserEntity object,String userId) {
		if(object.getId()==null){
			object.setCreateTime(StringUtil.getNowTime());
			object.setUpdateTime(StringUtil.getNowTime());
			object.setCreateUser(userId);
			object.setUpdateUser(userId);
			qsUserDao.save(object);
		}else{
			QsUserEntity qs = qsUserDao.getById(object.getId());
			qs.setUpdateUser(userId);
			qs.setUpdateTime(StringUtil.getNowTime());
			qs.setpName(object.getpName());
			qs.setBirthday(object.getBirthday());
			qs.setIdCard(object.getIdCard());
			qs.setSex(object.getSex());
			qs.setPatientNumber(object.getPatientNumber());
			qs.setPhoneNumber(object.getPhoneNumber());
			qs.setHospital(object.getHospital());
			qs.setAge(object.getAge());
			qsUserDao.saveOrUpdate(qs);
		}
	}

	@Override
	public HashMap<String, Object> getPage(QsUserEntity object, Integer pageNum, Integer pageSize) {
		HashMap<String, Object> map = new HashMap<>();
		List<QsUserEntity> res = page(object,pageNum,pageSize);
		Integer count = getAllRowCount(object);
		map.put("dataList",res);
		map.put("total",count);
		return map;
	}

	@Override
	public List<QsUserEntity> page(QsUserEntity tbDicNews, Integer pageNum, Integer pageSize) {
		String hql = " from QsUserEntity where 1=1 ";
		if(tbDicNews.getpName()!=null && !"".equals(tbDicNews.getpName())){
			hql += " and userName like '%" + tbDicNews.getpName() + "%' ";
		}
		return qsUserDao.page( hql,  pageNum,  pageSize);
	}

	@Override
	public List<QsUserEntity> list(QsUserEntity object) {
		return qsUserDao.list(object);
	}

	@Override
	public Integer getAllRowCount(QsUserEntity object) {
		String hql = " select count(*) from QsUserEntity where 1=1 ";
		if(object.getpName()!=null && !"".equals(object.getpName())){
			hql += " and userName like '%" + object.getpName() + "%' ";
		}
		return qsUserDao.getAllRowCount(hql);
	}


	@Override
	public void delete(QsUserEntity object) {
		qsUserDao.delete(object);
	}

	@Override
	public QsUserEntity selectOne(QsUserEntity object) {
		return qsUserDao.selectOne( object);
	}

	@Override
	public QsUserEntity getById(String id) {
		return qsUserDao.getById(id);
	}

	@Override
	public void saveQsUserEntity(QsUserEntity object,String userId) {
		object.setCreateTime(StringUtil.getNowTime());
		object.setUpdateTime(StringUtil.getNowTime());
		object.setCreateUser(userId);
		object.setUpdateUser(userId);
		qsUserDao.save(object);

	}

	@Override
	public AjaxJson checkUsername(String username) {
		AjaxJson ajaxJson=new AjaxJson();
		String hql=" from QsUserEntity where pName='"+username+"'";
		List<QsUserEntity> list=qsUserDao.getList(hql);
		if(list.size()==0){
			ajaxJson.setCode(200);
		}else {
			ajaxJson.setCode(201);
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson saveQsUser(QsUserEntity qsUserEntity) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			qsUserDao.save(qsUserEntity);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}

		return ajaxJson;
	}

	@Override
	public QsUserEntity login(QsUserEntity qsUserEntity) {
		QsUserEntity qsUserEntity1=null;
		String hql=" from QsUserEntity where pName='"+qsUserEntity.getpName()+"' and password='"+qsUserEntity.getPassword()+"'";
		List<QsUserEntity> list=qsUserDao.getList(hql);
		if(list.size()!=0){
			qsUserEntity1=list.get(0);
			return qsUserEntity1;
		}else{
			return qsUserEntity1;
		}

	}


}
